package com.top.horizon.th1804app.manager;

import com.net.protocol.model.FileTag;
import com.net.protocol.model.RecordFile;

import java.util.ArrayList;
import java.util.List;

public class FileManage {
    private FileManage() {
    }

    private static final FileManage sInstance = new FileManage();

    public static FileManage getInstance() {
        return sInstance;
    }

    private List<RecordFile> mData = new ArrayList<>();
    private List<RecordFile> mPlayData = new ArrayList<>();

    public boolean isList =false;
    /**
     * 获取全部列表，用于搜索结果展示
     *
     * @return
     */
    public List<RecordFile> getResultData() {
        return mData;
    }

    /**
     * 用于播放界面获取数据
     *
     * @return
     */
    public List<RecordFile> assemblePlayData() {
        mPlayData.clear();
        for (int i = 0; i < mData.size(); i++) {
            RecordFile recordFile = mData.get(i);
            if (recordFile.isSelected) {
                mPlayData.add(recordFile);
            }
        }
        mPlayData.get(0).playState = 1;
        return mPlayData;
    }

    public List<RecordFile> getPlayData(){
        return mPlayData;
    }

    public void setData(List<RecordFile> files) {
        if (files == null) {
            return;
        }
        mData = files;
    }

    public void clear() {
        mData.clear();
    }

    public int getSelectedSize() {
        int result = 0;
        for (int i = 0; i < mData.size(); i++) {
            if (mData.get(i).isSelected) {
                result++;
            }
        }
        return result;
    }

    public void putDeleteFile(String[] strings) {
        int index = 0;
        for (int i = 0; i < strings.length; i++) {
            for (; index < mData.size(); index++) {
                if (mData.get(index).isSelected) {
                    strings[i] = mData.get(index).name;
                    break;
                }
            }
        }
    }

    public void removeSelected() {
        List<RecordFile> delete = new ArrayList<>();
        for (int i = 0; i < mData.size(); i++) {
            RecordFile file = mData.get(i);
            if (file.isSelected) {
                delete.add(file);
            }
        }
        mData.removeAll(delete);
    }

    public void setAllSelected() {
        for (int i = 0; i < mData.size(); i++) {
            RecordFile file = mData.get(i);
            file.isSelected = true;
        }
    }

    public int getPlayDataSize() {
        return mPlayData.size();
    }

    public void changePlayFile(String playFileName, int position) {
        for (int i = 0; i < mPlayData.size(); i++) {
            RecordFile file = mPlayData.get(i);
            if (file.name.equals(playFileName)) {
                file.playState = 2;
                file.playPosition = position;
            }else {
                file.playPosition = 0;
                if (file.playState == 2){
                    file.playState =3;
                }else {
                    file.playState = 0;
                }
            }
        }
    }

    public void addFileTag(List<FileTag.Tag> tags) {
        if (tags == null || tags.size() ==0){
            return;
        }
        String fileName = tags.get(0).name;
        for (int i = 0; i < mPlayData.size(); i++) {
            RecordFile file = mPlayData.get(i);
            if (file.name.equals(fileName)) {
                file.tags = tags;
                break;
            }
        }
    }

    public void removeFromPlay(int position, RecordFile file) {
        if (mPlayData.size() > position){
            mPlayData.remove(position);
        }
    }

    public RecordFile getPlaySelected() {
        for (int i = 0; i < mPlayData.size(); i++) {
            RecordFile file = mPlayData.get(i);
            if (file.isSelected) {
                return file;
            }
        }
        return null;
    }

    public int getPlaySelectedIndex() {
        for (int i = 0; i < mPlayData.size(); i++) {
            RecordFile file = mPlayData.get(i);
            if (file.isSelected) {
                return i;
            }
        }
        return 0;
    }
}
